package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.mapper.UserMapper;
import com.example.demo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 你爸爸
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2023-12-19 17:07:20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int register(User user) {
        if (userMapper.findByUsername(user.getName()) != null) {
            throw new RuntimeException("Username already exists");
        }
        return userMapper.insert(user);
    }

    @Override
    public Page<UserVo> selectPage(Page<User> page) {
        Page<UserVo> resPage = new Page<>(page.getCurrent(), page.getSize());
        List<UserVo> list = new ArrayList<>();
        System.out.println(page);
        page = userMapper.selectPage(page, null);
        for (User record : page.getRecords()) {
            list.add(UserVo.builder()
                    .id(record.getId())
                    .avatar(record.getAvatar())
                    .name(record.getName())
                    .account(record.getAccount())
                    .roleId(record.getRoleId())
                    .phone(record.getPhone())
                    .mailbox(record.getMailbox())
                    .wechat(record.getWechat())
                    .qq(record.getQq())
                    .loginTime(record.getLoginTime())
                    .logoutTime(record.getLogoutTime())
                    .createTime(record.getCreateTime())
                    .build());
        }
        resPage.setRecords(list);
        resPage.setTotal(page.getTotal());
        resPage.setPages(page.getPages());
        return resPage;
    }


}




